//
//  BookInfoViewControllerA.m
//  Unicorn
//
//  Created by xalo on 16/4/29.
//  Copyright © 2016年 bhj_jun. All rights reserved.
//

#import "BookInfoViewControllerA.h"
#import "BookInfoViewControllerB.h"

#import "BookAModel.h"
#import "BookACell.h"


#define kURLa @"http://law.anywebapps.com/law/api.php"
                //参数（tasks=lawsClassList & language=0）

@interface BookInfoViewControllerA ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)NSMutableDictionary *dataSourceDic;//源数据
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *dataSourceArr;


@end

@implementation BookInfoViewControllerA

- (void)viewDidLoad {
    [super viewDidLoad];
    [self viewSetting];
    
    self.navigationItem.title = @"法律资料库";
    
    self.tableView.rowHeight = 60;
    
//    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 10)];
//    self.tableView.tableHeaderView.backgroundColor = [UIColor blackColor];
    
//    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, kHeight, kWidth, 5)];
//    self.tableView.tableFooterView.backgroundColor = [UIColor clearColor];
    

    
    //数据请求调用
    [self requestDataWithUrl:kURLa parameter:self.dataSourceDic];
    
    //注册单元格
    [self.tableView registerNib:[UINib nibWithNibName:@"BookACell" bundle:nil] forCellReuseIdentifier:@"BookACell"];

    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - - 懒加载
-(NSMutableDictionary *)dataSourceDic
{
    if (!_dataSourceDic) {
        _dataSourceDic=[NSMutableDictionary dictionaryWithDictionary:@{@"tasks":@"lawsClassList",@"language":@(0)}] ;
    }
    return _dataSourceDic;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 20, kWidth, kHeight-100+48)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
    }
    return _tableView;
}

-(NSMutableArray *)dataSourceArr
{
    if (!_dataSourceArr) {
        _dataSourceArr=[NSMutableArray array];
    }
    return _dataSourceArr;
}


-(void)viewSetting
{
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageView.contentMode = UIViewContentModeScaleToFill;
    [imageView setImage:[UIImage imageNamed:@"cyhd.net_7.jpg"]];
    self.view.backgroundColor = [UIColor colorWithPatternImage:imageView.image];
//    [self.view addSubview:imageView];
    
    [self.view addSubview:self.tableView];
    
}




#pragma mark - - 数据请求
-(void)requestDataWithUrl:(NSString *)url parameter:(NSDictionary *)parameter
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject
                                                            options:NSJSONReadingAllowFragments error:nil];
        NSArray *arr = dic[@"tasks"];
        for (NSDictionary *dicB in arr) {
            BookAModel *model = [[BookAModel alloc] init];
            [model setValuesForKeysWithDictionary:dicB];
            [self.dataSourceArr addObject:model];
            
        }
//NSLog(@"self.datasourcearr-->%@",self.dataSourceArr);
//NSLog(@"BookA--数据请求成功__%@",dic);
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"BookA－－数据请求失败");
    }];
}

#pragma mark - - tableView代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    BookACell *cell = [tableView dequeueReusableCellWithIdentifier:@"BookACell" forIndexPath:indexPath];

    BookAModel *model = self.dataSourceArr[indexPath.row];

        cell.textLabel.text = (NSString *)model.className;
        
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        tableView.showsHorizontalScrollIndicator = YES;
    
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    
        cell.opaque = NO;
        
        cell.textLabel.backgroundColor = [UIColor clearColor];
        
        [cell setBackgroundColor:[UIColor colorWithRed:5 green:1 blue:1 alpha:0.2]];
 
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BookInfoViewControllerB *bookVCB = [[BookInfoViewControllerB alloc]init];
    [self.navigationController pushViewController:bookVCB animated:YES];
    BookAModel *model = self.dataSourceArr[indexPath.row];
    bookVCB.passid =model.bookid;
    
    NSLog(@"点击零");
}

@end
